package com.uroad.jilinpda.layer;

import android.app.Activity;
import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;

import com.uroad.jilinpda.common.CurrApplication;
import com.uroad.jilinpda.common.GlobalData;
import com.uroad.jilinpda.http.OkHttpNetWork;
import com.uroad.jilinpda.http.OkHttpNetWorkCallBack;
import com.uroad.jilinpda.modle.UserMDL;
import com.uroad.jilinpda.util.SPUtils;
import com.uroad.jilinpda.webservice.ActivateWS;
import com.uroad.jilinpda.widget.TimeCountController;
import com.uroad.lib.json.FastJsonUtils;
import com.uroad.lib.util.encrypt.SecurityUtil;
import com.uroad.lib.widget.dialog.DialogHelper;

import static com.uroad.jilinpda.webservice.BaseWS.getNewUrl;

public class LoginLayer {

    private Activity context;
    private LoginLayerCallBack callBack;
    private OkHttpNetWork netWork;//网络请求类
    private LoginNetWorkCallBack netWorkCallBack;
    private TimeCountController timeCountController;
    private String deviceuuid;
    private String phone;
    private String pwd;

    public LoginLayer(Activity context, LoginLayerCallBack callBack){
        this.context = context;
        this.callBack = callBack;
        netWork = new OkHttpNetWork(context);
        netWorkCallBack = new LoginNetWorkCallBack();
        initTimeCountController();
    }

    private void initTimeCountController(){
        timeCountController = new TimeCountController(60*1000, 1*1000,
                new TimeCountController.OnTimeCountListener() {
                    @Override
                    public void onTick(long time) {
                        callBack.timeControllerCallBack("重新发送("+time/1000 + "s)", time);
//                        tv_getCode.setText("重新发送("+time/1000 + "s)");
                    }

                    @Override
                    public void onFinish() {
//                        tv_getCode.setClickable(true);
//                        tv_getCode.setText("获取验证码");
                        callBack.timeControllerCallBack("获取验证码", -1);
                    }

                    @Override
                    public void onCancel() {

                    }
                });
    }
    /**
     * 验证码接口
     */
    public String sendCode(String phone){
        if (TextUtils.isEmpty(phone)) {
            return "请输入手机号";
        }
        if (phone.length() != 11) {
            return "请输正确入手机号";
        }
        deviceuuid = CurrApplication.getInstance().getDeviceuuid();
        if (TextUtils.isEmpty(deviceuuid)) {
            return"获取设备id失败";
        }
        timeCountController.start();
        netWork.doAllUrlRequest(getNewUrl(ActivateWS.sendCode), ActivateWS.sendCode(phone, deviceuuid), ActivateWS.sendCode, netWorkCallBack);
        return "";
    }

    /**
     * 验证码接口
     */
    public String login(String phone, String pwd){
        this.phone = phone;
        if (TextUtils.isEmpty(phone)) {
            return"请输入手机号";
        }
        if (phone.length() != 11) {
            return "请输正确入手机号";
        }
        if (TextUtils.isEmpty(pwd)) {
            return"请输入验证码";
        }
        String password;
        try {
            password = SecurityUtil.EncoderByMd5(pwd);
        } catch (Exception e) {
            e.printStackTrace();
            return"MD5加密异常";
        }
        deviceuuid = CurrApplication.getInstance().getDeviceuuid();
        if (TextUtils.isEmpty(deviceuuid)) {
            return"获取设备id失败";
        }
        DialogHelper.showLoading(context,"正在登录");
        netWork.doAllUrlRequest(getNewUrl(ActivateWS.login), ActivateWS.loginParams(phone, password, deviceuuid), ActivateWS.login, netWorkCallBack);
        return "";
    }

    private void loginSuccess(JSONObject jsonObject) {
        String data = FastJsonUtils.getData(jsonObject);
        UserMDL user = FastJsonUtils.parseObjectJSON(data, UserMDL.class);
        if (user != null) {
            CurrApplication.getInstance().setUserLoginer(user);
            //记录登录状态
            SPUtils.put(context, GlobalData.sp_ticketid, user.getTicketid());
            SPUtils.put(context, GlobalData.sp_useruuid, user.getUseruuid());
            SPUtils.put(context, GlobalData.sp_phone, user.getPhone());
            SPUtils.put(context, GlobalData.sp_username, user.getUsername());
            SPUtils.put(context, GlobalData.sp_icon, user.getIcon());
            SPUtils.put(context, GlobalData.sp_recocode, user.getRecocode());
//            SPUtils.put(context, GlobalData.sp_loginphone, phone);
            SPUtils.put(context, GlobalData.sp_usertype, user.getUsertype());
            SPUtils.put(context, GlobalData.sp_number, user.getNumber());
            /*if (cbSavePwd.isChecked()) {
                SPUtils.put(context, GlobalData.sp_loginphone, phone);
                SPUtils.put(context, GlobalData.sp_password, pwd);
            }else{
                SPUtils.remove(context, GlobalData.sp_loginphone);
                SPUtils.remove(context, GlobalData.sp_password);
            }*/

            //登录成功
            CurrApplication.user = user;
            if (callBack != null)
                callBack.onSuccess();
        } else {
            if (callBack != null)
                callBack.onFailure("未能获取到登录对象");
        }
    }
    /**
     * 接口回调
     */
    private class LoginNetWorkCallBack implements OkHttpNetWorkCallBack {

        @Override
        public void OnHttpNetWork(String type) {
            if (callBack != null)
                callBack.onFailure(type+"NetWork");
        }

        @Override
        public void OnHttpFailure(String type, String msg) {
            if (callBack != null)
                callBack.onFailure(msg);
        }

        @Override
        public void OnHttpTaskComplete(String result, String type) {
            JSONObject jsonObject = JSONObject.parseObject(result.toString());
            if (FastJsonUtils.GetJsonStatu1(jsonObject)) {
                if (ActivateWS.login.equalsIgnoreCase(type)) {
                    loginSuccess(jsonObject);
                }
            }else{
                if (callBack != null)
                    callBack.onFailure(jsonObject.getString("msg"));
            }
        }

    }

    public interface LoginLayerCallBack extends LayerCallBack{
        void timeControllerCallBack(String resutl, long time);//检测结果
    }
}
